<template>
  <a-dropdown v-if="currentUser && currentUser.name" placement="bottomRight">
    <span class="ant-pro-account-avatar">
      <a-avatar
        size="small"
        :src="$store.getters.avatar != '' ? $store.getters.avatar : avatar"
        class="antd-pro-global-header-index-avatar"
      />
      <span>{{ currentUser.name }}</span>
    </span>
    <template v-slot:overlay>
      <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
        <!-- <a-menu-item v-if="menu" key="center" @click="handleToCenter">
          <a-icon type="user" />
          个人中心
        </a-menu-item> -->
        <!-- <a-menu-item v-if="menu" key="settings" @click="handleToSettings">
          <a-icon type="setting" />
          修改密码
        </a-menu-item> -->
        <a-menu-divider v-if="menu" />
        <a-menu-item key="logout" @click="handleLogout">
          <a-icon type="logout" />
          退出登录
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
  <span v-else>
    <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
  </span>
</template>

<script>
import { Modal } from 'ant-design-vue'
import { loginOut, userOut } from '@/api/user'
import { removeToken } from '@/utils/util.js'
export default {
  name: 'AvatarDropdown',
  data() {
    return {
      avatar: require('../../assets/defaultTX.png'),
    }
  },
  props: {
    currentUser: {
      type: Object,
      default: () => null,
    },
    menu: {
      type: Boolean,
      default: true,
    },
  },
  methods: {
    handleToSettings() {
      this.$router.push({ path: '/user/password' })
    },
    handleLogout(e) {
      Modal.confirm({
        title: '提示',
        content: '确定要退出吗？',
        onOk: () => {
          userOut({})
            .then((res) => {
              console.log(res)
              this.$notification.success({
                message: '退出登录成功',
                description: '',
              })
              removeToken('pssid')
              setTimeout(() => {
                window.location.reload()
              }, 1500)
            })
            .catch((err) => {})
        },
        onCancel() {},
      })
    },
  },
}
</script>

<style lang="less" scoped>
.ant-pro-drop-down {
  /deep/ .action {
    margin-right: 8px;
  }
  /deep/ .ant-dropdown-menu-item {
    min-width: 160px;
  }
}
</style>
